package com.easymin.weixin.controller.res;

import com.easymin.weixin.service.WeixinQrCodeService;
import com.easymin.weixin.service.WxUserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhyu
 * @date 2019-07-13 17:10:07
 */
@Controller
@RequestMapping("api")
public class WeixinInfoController {

    private Logger log = LoggerFactory.getLogger(WeixinInfoController.class);

    @Autowired
    private WeixinQrCodeService weixinQrCodeService;

    @Autowired
    private WxUserInfoService wxUserInfoService;

    public static final String SUCCESS = "success";

    public static final String DATA = "data";


    @RequestMapping("getWxUserInfoByPhones")
    @ResponseBody
    public Map<String, Object> getWeixinUserInfoByPhone(String[] phones) {
        Map<String, Object> re = new HashMap<>();
        try{
            if(null != phones && 1 == phones.length){
               Map<String, String> data = wxUserInfoService.getWeixinUserInfoByPhone(phones[0]);
                re.put(DATA, data);
            }else {
                Map<String, Map<String, String>> data = wxUserInfoService.getWeixinUserInfoByPhone(phones);
                re.put(DATA, data);
            }
            re.put(SUCCESS, true);
        }catch (Exception e){
            re.put(SUCCESS, false);
        }
        return re;
    }

    @RequestMapping("shareCode/getWeixinShareQrCode")
    @ResponseBody
    public Map<String, Object> getWeixinShareQrCode(String driverId, String name) {
        Map<String, Object> map = new HashMap<>();
        if (null != driverId) {
            try {
                String[] data = weixinQrCodeService.getWeixinQrCodeService(driverId, name);
                if (null != data) {
                    map.put("success", true);
                    map.put("accessToken", data[2]);
                    map.put("url", data[1]);
                    map.put("ticket", data[0]);
                    return map;
                }
            } catch (Exception e) {
                e.printStackTrace();
                map.put("success", false);
                map.put("message", "error");
                return map;
            }

        }
        map.put("SUCCESS", false);
        map.put("message", "driverId is null");
        return map;

    }
}
